package com.schoolhelper.controller.adapter;

import android.graphics.Bitmap;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.schoolhelper.controller.activity.R;
import com.schoolhelper.model.adaptermodel.DetailRecyclerViewDiscuss;
import com.schoolhelper.utils.ImageUtils;
import com.schoolhelper.utils.RelativeDateFormat;

import java.util.List;

public class DetailRecyclerViewDiscussAdapter extends RecyclerView.Adapter<DetailRecyclerViewDiscussAdapter.ViewHolder> implements View.OnClickListener {

    private int contentType = 2;

    private List<DetailRecyclerViewDiscuss> contentsList;

    private OnItemClickListener itemClickListener;

    private Bitmap profileBitmap = null;

    public DetailRecyclerViewDiscussAdapter(List<DetailRecyclerViewDiscuss> contents) {
        contentsList = contents;
    }

    public int getContentType() {
        return contentType;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView profileImageView;
        TextView dateTextView;
        TextView textTextView;
        TextView usernameTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            profileImageView = (ImageView) itemView.findViewById(R.id.detail_discuss_user_img);
            dateTextView = (TextView) itemView.findViewById(R.id.detail_discuss_date);
            textTextView = (TextView) itemView.findViewById(R.id.detail_discuss_text);
            usernameTextView = (TextView) itemView.findViewById(R.id.detail_discuss_username);

        }
    }

    @Override
    public void onClick(View v) {
//        if (itemClickListener != null){
//            itemClickListener.onItemClick((Integer) v.getTag(), contentType);
//        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_discuss_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        DetailRecyclerViewDiscuss content = contentsList.get(position);
        String profileImageUrl = content.getProfileImageUrl();
        if (profileBitmap == null) {
            profileBitmap = ImageUtils.GetLocalOrNetBitmap(profileImageUrl);
        }
        holder.profileImageView.setImageBitmap(profileBitmap);

        holder.dateTextView.setText(RelativeDateFormat.date2String(content.getDiscussDatetime()));
        holder.textTextView.setText(content.getDiscussText());
        holder.usernameTextView.setText(content.getUsername());

        holder.itemView.setTag(position);


    }

    @Override
    public int getItemCount() {
        return contentsList.size();
    }

    public void setItemClickListener(OnItemClickListener onItemClickListener, int mcontentType) {
        itemClickListener = onItemClickListener;
    }

}
